package cn.com.sinux.spring.utils.fileconvert;

import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.xslf.usermodel.XMLSlideShow;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;

/**
 * Created by Administrator on 2016/10/6.
 */
public class POIConvertEngine extends ConvertEngine {


    public void convert(ConvertType convertType, ConvertObject convertObject) throws Exception {

    }

    //    2007 doc
    public static void word2007ToHtml() throws IOException {
//        File f = new File("d:/test/test.docx");
//        // 1) Load DOCX into XWPFDocument
//        InputStream in = new FileInputStream(f);
//        XWPFDocument document = new XWPFDocument(in);
//
//        // 2) Prepare XHTML options (here we set the IURIResolver to
//        // load images from a "word/media" folder)
//        File imageFolderFile = new File("d:/test/media");
//        XHTMLOptions options = XHTMLOptions.create().URIResolver(
//                new FileURIResolver(imageFolderFile));
//        options.setExtractor(new FileImageExtractor(imageFolderFile));
//        //options.setIgnoreStylesIfUnused(false);
//        //options.setFragment(true);
//
//        // 3) Convert XWPFDocument to XHTML
//        OutputStream out = new FileOutputStream(new File(
//                "d:/test/test.htm"));
//        XHTMLConverter.getInstance().convert(document, out, options);
    }

    //    2003 doc
    public void wordToHtml(ConvertObject convertObject) throws Exception {
        InputStream is = new FileInputStream(convertObject.getSource());
        HWPFDocument wordDocument = new HWPFDocument(is);
        WordToHtmlConverter converter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
        //对HWPFDocument进行转换
        converter.processDocument(wordDocument);
        Writer writer = new FileWriter(new File(convertObject.getDesDir() + "/" + "converter.html"));
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" );
        //是否添加空格
        transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
        transformer.setOutputProperty( OutputKeys.METHOD, "html" );
        transformer.transform(
                new DOMSource(converter.getDocument() ),
                new StreamResult( writer ) );
        convertObject.success("converter.html");
    }

    private void toImage2007(ConvertObject convertObject) {
        try {
            FileInputStream is = new FileInputStream(convertObject.getSource());
            XMLSlideShow ppt = new XMLSlideShow(is);
            is.close();

            Dimension pgsize = ppt.getPageSize();

            java.util.List<String> results = new ArrayList<String>();
            for (int i = 0; i < ppt.getSlides().size(); i++) {
                BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics = img.createGraphics();
                // clear the drawing area
                graphics.setPaint(Color.white);
                graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

                // render
                ppt.getSlides().get(i).draw(graphics);

                // save the output
                String filename = "07-" + (i + 1) + ".png";

                FileOutputStream out = new FileOutputStream(convertObject.getDesDir() + "/" + filename);
                javax.imageio.ImageIO.write(img, "png", out);
                results.add(filename);
                out.close();
            }
            convertObject.success(results);
        } catch (Exception e) {

        }
    }

    public static void toImage2003(ConvertObject convertObject) {
        try {
            FileInputStream is = new FileInputStream(convertObject.getSource());
            HSLFSlideShow ppt = new HSLFSlideShow(is);
            is.close();

            Dimension pgsize = ppt.getPageSize();
            java.util.List<String> results = new ArrayList<String>();
            for (int i = 0; i < ppt.getSlides().size(); i++) {
                BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics = img.createGraphics();
                // clear the drawing area
                graphics.setPaint(Color.white);
                graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

                // render
                ppt.getSlides().get(i).draw(graphics);

                // save the output
                String filename = "03-" + (i + 1) + ".png";
                FileOutputStream out = new FileOutputStream(convertObject.getDesDir() + "/" + filename);
                javax.imageio.ImageIO.write(img, "png", out);
                results.add(filename);
                out.close();
            }
            convertObject.success(results);
        } catch (Exception e) {

        }
    }
}
